package cn.csg.iotplatform.module.system.controller.admin.metadata;

import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.module.system.controller.admin.metadata.vo.MetaDataPageReqVO;
import cn.csg.iotplatform.module.system.controller.admin.metadata.vo.MetaDataSaveReqVO;
import cn.csg.iotplatform.module.system.dal.dataobject.metadata.MetaDataDO;
import cn.csg.iotplatform.module.system.service.metadata.MetaDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 前端元数据存储")
@RestController
@RequestMapping("/system/metaData")
public class MetaDataController {

    @Resource
    private MetaDataService metaDataService;

    @PostMapping("/createOrSaveMetaData")
    @Operation(summary = "创建前端元数据存储")
    public CommonResult<String> createMetaData(@Valid @RequestBody MetaDataSaveReqVO createReqVO) {
        metaDataService.createOrSaveMetaData(createReqVO);
        return success("操作成功");
    }

    @DeleteMapping("/deleteMetaDataByIdOrType")
    @Operation(summary = "删除前端元数据存储")
    @Parameters({@Parameter(name = "id", description = "iD", required = false),
        @Parameter(name = "type", description = "类型", required = false)})
    public CommonResult<Boolean> deleteMetaDataByIdOrType(String id, String type) {
        if (id == null && type == null) {
            return CommonResult.error(500, "id和类型必传一个");
        }
        if (id != null) {
            metaDataService.deleteMetaDataById(id);
        } else {
            metaDataService.deleteMetaDataByType(type);
        }
        return success(true);
    }

    @GetMapping("/getMetaDataByIdOrType")
    @Operation(summary = "获得前端元数据存储根据id")
    @Parameters({@Parameter(name = "id", description = "iD", required = false),
        @Parameter(name = "type", description = "类型", required = false)})
    public CommonResult<MetaDataDO> getMetaDataById(String id, String type) {
        if (id == null && type == null) {
            return CommonResult.error(500, "id和类型必传一个");
        }
        MetaDataDO metaDataDO;
        if (id != null) {
            metaDataDO = metaDataService.getMetaDataById(id);
        } else {
            metaDataDO = metaDataService.getMetaDataByType(type);
        }
        return success(metaDataDO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得前端元数据存储分页")
    public CommonResult<PageResult<MetaDataDO>> getMetaDataPage(@Valid MetaDataPageReqVO pageReqVO) {
        PageResult<MetaDataDO> pageResult = metaDataService.getMetaDataPage(pageReqVO);
        return success(pageResult);
    }

}